#coding:utf8
import requests
import re
from lxml import etree

# 使用正则表达式匹配出字体样式中的 url 链接
# TODO： 评论区的信息并没有加载到网页当中(请求头问题)
# 请求头
headers = {
            'Connection': 'keep-alive',
            'Host': 'www.dianping.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
            'Cookie': '_lxsdk_cuid=173ec1d725e80-001e5367555543-7e647c65-1fa400-173ec1d725fc8; _lxsdk=173ec1d725e80-001e5367555543-7e647c65-1fa400-173ec1d725fc8; _hc.v=9a0b663c-e5a8-7e01-077a-b8d4d7220afa.1597394220; s_ViewType=10; fspop=test; dper=7c9cd1b645ff07fe1fd65310a52d0eca184842b7256f2ef6b5bcb5de6f1d50d57f591dac67305a8997d64b4f32f280b3bb2f74d58210d10a0fcc57a335a1c1618e36e6a24b0e7784c40ef3a2583a5b8c7a65df07c94c25578fa6e7223abeec06; ua=dpuser_5191363077; ctu=d2a46ed772193566de2cd33b3ff8ea407fbae2e64a575314195403e2ec7f168a; cy=2; cye=beijing; ll=7fd06e815b796be3df069dec7836c3df; _lx_utm=utm_source%3Dwww.sogou%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1612071553,1612071565,1612071610,1612092249; dplet=e77caddcb23d0ee50d83ede34e65a5da; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1612102455; _lxsdk_s=17758c69b55-16e-a09-efe%7C%7C173'
}

headers2 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
}
# 网址
url = 'http://www.dianping.com/shop/H9FNPpCqj1Tu98oD/review_all'

# 发起请求
r = requests.get(url=url, headers=headers)
# 如果状态码为200显示正常
if r.status_code == 200:
    print("访问成功")
    text = r.text.encode("gbk", "ignore").decode("gbk", "ignore")  # 解决报错双重严格限制
    # 获取存放映射关系css链接
    css_url = re.search('<link rel="stylesheet" type="text/css" href="(//s3plus.meituan.*?)"', text)[1]
    # 拼接css_rul
    total_css_url = 'http:' + css_url
    print(total_css_url)
    # TODO: 404 r1，换个请求头
    r1 = requests.get(url=total_css_url, headers=headers2)
    print(r1.status_code)
    text1 = r1.text.encode("gbk", "ignore").decode("gbk", "ignore")  # 解决报错双重严格限制
    '''
    页面中的加密字体的 css 代码示例
    svgmtsi[
    class ^="qfs"] {
    width: 14
    px;
    height: 24
    px;
    margin - top: -14
    px;
    background - image: url( // s3plus.meituan.net / v1 / mss_0a06a47… / svgtextcss / 60
    ff9a5….svg);
    background - repeat: no - repeat;
    display: inline - block;
    vertical - align: middle;
    }
    '''
    # svgmtsi[class^="qfs"]{width: 14px;height: 24px;margin-top: -14px;background-image: url(//s3plus.meituan.net/v1/mss_0a06a471f9514fc79c981b5466f56b91/svgtextcss/60ff9a50fd0d7753c8ebc0178004f283.svg)
    # \：转译符号
    # 匹配得出 背景的 svg 的 url
    svg_url = re.findall(r'svgmtsi\[class\^="qfs"\].*?background-image: url\((.*?)\)', text1)
    # print(svg_url)
    # 加一个http  得到字体映射字典
    total_svg_url = 'http:' + svg_url[0]
    print(total_svg_url)
else:
     print("被反爬了！小夜斗赶紧跑路！")

